package com.menggudf.common.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.binary.Base64;

public class EncodeExcelFileNameUtils {
    private static final String FILE_NAME = "数据表.xlsx";

    public static String encodingFileName(HttpServletRequest request,
                                          String fileName) throws UnsupportedEncodingException {
        if (fileName == null || "".equals(fileName.trim())) {
            fileName = FILE_NAME;
        }
        String agent = request.getHeader("USER-AGENT");
        if (null != agent && -1 != agent.indexOf("MSIE")) {
            return URLEncoder.encode(fileName, "UTF-8");
        } else if (null != agent && -1 != agent.indexOf("Mozilla")) {
            return "=?UTF-8?B?"
                    + (new String(Base64.encodeBase64(fileName
                    .getBytes("UTF-8")))) + "?=";
        } else {
            return fileName;
        }
    }
}
